import { Widget, _decorator, Node, WebView, Component, EditBox, game, Label, tween, Vec3 } from 'cc';
import { EventManger } from './EventManger';
import { UIManager } from '../../script/UIManager';
const { ccclass, property, requireComponent } = _decorator;

@ccclass('appNode')
export class appNode extends Component {

     //隐私提示框
    @property(Node)
    public yinsiTipNode: Node = null;

    //隐私，用户政策
    @property(Node)
    public yinsiNode: Node = null;

    @property(WebView)
    public yisiWeb: WebView = null;

    //适龄提示
    @property(Node)
    public ageNode: Node = null;

    //实名认证
    @property(Node)
    public nameNode: Node = null;

    //防沉迷提示
    @property(Node)
    public tipNode: Node = null;

    //防沉迷关闭按钮
    @property(Node)
    public tipCloseBtn: Node = null;

    //提示
    @property(Node)
    public noticeTip: Node = null;


    @property(EditBox)
    public nameEdit: EditBox = null;

    @property(EditBox)
    public IdEdit: EditBox = null;

    yinsiUrl:string = "http://fh.admin.62.hzgqapp.com/PrivacyPolicy.html";

    yonghuUrl:string = "http://fh.admin.62.hzgqapp.com/UserAgreement.html";

    onLoad () {
        let isName = localStorage.getItem("nameIsReal")
        if (!isName){
            //this.yinsiTipNode.active = true;
        }
        EventManger.eventTarget.on("openNameNode", this.openNameNode, this);
        EventManger.eventTarget.on("showTip", this.showTip, this);
    }

    onDestroy(){
        EventManger.eventTarget.off("openNameNode", this.openNameNode, this);
        EventManger.eventTarget.off("showTip", this.showTip, this);
    }

    openNameNode(){
        this.nameNode.active = true
    }

    onBtnSubmit() {
        if (this.nameEdit.string == "" || this.IdEdit.string == ""){
            this.showTip("请填写正确的身份信息");
            return;
        }
        if(this.IdEdit.string.length != 18){
            this.showTip("请填写正确的身份信息");
            return;
        }

        localStorage.setItem("nameIsReal", "true")
        this.showTip("认证通过");

        this.nameNode.active = false;
        this.tipNode.active = true;

        // http.regisName(this.nameEdit.string, this.IdEdit.string, (data)=> {
        //     console.log("sendAdInfoCallBack:" + JSON.stringify(data));
        //     if (data.code == 200){
        //         event.setUserData("验证成功");
        //         this.node.dispatchEvent(event);
        //         this.node.active = false;
        //     }else if (data.code == 302){
        //         event.setUserData("验证成功，但属于未成年");
        //         this.node.dispatchEvent(event);
        //         this.node.active = false;

        //         let event2 = new cc.Event.EventCustom("showAntiAddiction",true);
        //         this.node.dispatchEvent(event2);
        //     }else{
        //         event.setUserData("认证失败");
        //         this.node.dispatchEvent(event);
        //     }
            
        // }, ()=>{
        //     event.setUserData("认证失败");
        //     this.node.dispatchEvent(event);
        // });

    }

    onBtnClick(event:Event, str:string){
        if (str == "closeYinsi"){
            this.yinsiNode.active = false;
        }
        else if (str == "closeYinsiTip"){
            this.yinsiTipNode.active = false;
        }
        else if (str == "closeNameNode"){
            this.nameNode.active = false;
        }
        else if (str == "closeTipNode"){
            this.tipNode.active = false;
        }
        else if (str == "closeAgeNode"){
            this.ageNode.active = false;
        }
        else if (str == "openNameNode"){
            this.nameNode.active = false;
        }
        else if (str == "openTipNode"){
            this.tipNode.active = false;
        }
        else if (str == "openYinsi"){
            this.yinsiNode.active = true;
            this.yisiWeb.url = this.yinsiUrl
        }
        else if (str == "openYonghu"){
            this.yinsiNode.active = true;
            this.yisiWeb.url = this.yonghuUrl
        }
        else if (str == "openAgeNode"){
            this.ageNode.active = true;
        }
        else if (str == "end"){
            if (game){
                game.end();
            }
        }
        
    }
    
    showTip(str:string){
        UIManager.showTip(str);
        // this.noticeTip.getChildByName('Label').getComponent(Label).string = str;
        // tween(this.noticeTip)
        //     .to(0.1,{scale: new Vec3(1,1,1)})
        //     .delay(1.5)
        //     .to(0.1,{scale: new Vec3(0,0,1)})
        //     .start()
    }

}


